 

function dangerManagementCtrl($rootScope, $scope, $http, ngDialog, paFetchService, ngDialogConfirm) {
  'ngInject';
  this.queryData = {};
  this.paginationQuery = function() {};

  // 分页
  this.paginationData = {
    pageNum: 1,
    pageSize: 10,
    total: 4,
  }

  this.dangerManagementList = [
    {
      id: 1,
      area: '临时用电区域',
      address: '一标ZD-200桩',
      status: true,
      dangerItemList: [
        {
          name: '配电箱',
          grade: 'C',
          behave: '配电箱',
          factor: '距地面距离不合要求',
          effect: '触电',
          prevent: '按临时用电规范要求',
        },
        {
          name: '配电开关',
          grade: 'C',
          behave: '配电开关',
          factor: '未两级漏保',
          effect: '触电',
          prevent: '按临时用电规范要求',
        },
      ],
    },
    {
      id: 2,
      area: '便道修建',
      address: '一标ZD-200桩',
      status: true,
      dangerItemList: [
        {
          name: '配电箱',
          grade: 'C',
          behave: '配电箱',
          factor: '距地面距离不合要求',
          effect: '触电',
          prevent: '按临时用电规范要求',
        },
        {
          name: '修土石便道',
          grade: 'B',
          behave: '便道修建',
          factor: '违章作业',
          effect: '高出坠落',
          prevent: '施工时加强安全规范',
        },
      ],
    },
  ]

  this.topChecked = false;
  this.toggleAll = () => {
    // if (this.topChecked) {
      this.dangerManagementList.forEach((item) => {
        item.checked = this.topChecked
      })
    // }
  }

  this.printQrCode = () => {
    let selectedItems = this._getSelectedItems();
    if (!selectedItems.length) {
      layer.alert('请选择危险源');
      return;
    }
    $scope.selectedItems = selectedItems;
    ngDialog.openConfirm({
      template: 'dangerManagementQrCode',
      className: 'ngdialog-theme-default pa-confirm-theme',
      scope: $scope,
      controllerAs: 'vm',
      controller: function($scope) {
        'ngInject';
        this.selectedItems = selectedItems;
        console.log(selectedItems)
        this.print = () => {
          $('.qr-print').print();
        }

        this.getQrCodeSrc = (item) => {
          let qrContent = encodeURIComponent(`#/dangerSource/${item.id}`);
          let requestUrlStr = 
          `${$scope.serverContext}/api/qr?size=160&code=${qrContent}`;
          return requestUrlStr;
        }
      },
    })
  }

  this._getSelectedItems = () => {
    let res = [];
    this.dangerManagementList.forEach((item) => {
      if (item.checked) {
        res.push(item);
      }
    })
    return res;
  }

  // 重置
  this.reset = () => {
    this.queryData = {
    	queryType: 0,
      bidId: $rootScope.activeBid.bidId,
    };
  }

  // 查询
  this.query = (param) => {
	  this.paginationQuery();
  }

  this.checkDetail = (detail) => {
    let _this = this;
    ngDialog.open({
      template: 'dangerDetail',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme',
      controllerAs: 'detail',
      scope: $scope,
      controller: function trainClarifyCtrl($scope, $http) {
        'ngInject';
        this.model = detail;
        
        this.addDangerItem = () => {
          this.model.dangerItemList.push({});
        }
        this.deleteItem = ($index) => {
          this.model.dangerItemList.splice($index, 1);
        }
        this.save = () => {
          console.log(this.model);
          _this.dangerManagementList.push(this.model)
          $scope.closeThisDialog();
        }

      }
    });
  }

  this.deleteDetail = ($index) => {
    ngDialogConfirm
      .confirm()
      .then(() => {
        this.dangerManagementList.splice($index, 1);
      })
  }

  // 新增危险源
  this.addDialogForm = () => {
    let _this = this;
    ngDialog.open({
      template: 'addDangerForm',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme',
      controllerAs: 'add',
      scope: $scope,
      controller: function trainClarifyCtrl($scope, $http) {
        'ngInject';
        this.model = {
          id: _this.dangerManagementList[_this.dangerManagementList.length - 1].id + 1,
          area: '',
          address: '',
          dangerItemList: [
            {}
          ]
        }
        
        this.addDangerItem = () => {
          this.model.dangerItemList.push({});
        }
        this.deleteItem = ($index) => {
          this.model.dangerItemList.splice($index, 1);
        }
        this.save = () => {
          console.log(this.model);
          _this.dangerManagementList.push(this.model)
          $scope.closeThisDialog();
        }

      }
    });
  }

}

export default dangerManagementCtrl;